package cn.jorian.framework.util;

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

public class RestTemplateUtil {

    /**
     * post 表单请求
     * @param restTemplate
     * @param url
     * @param map
     * @param responseType
     * @return
     * @param <T>
     */
    public static <T> T postForObject(RestTemplate restTemplate, String url, MultiValueMap<String, Object> map, Class<T> responseType) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        HttpEntity<MultiValueMap<String,Object>> formEntity = new HttpEntity<>(map, headers);
        T result = restTemplate.postForObject(url, formEntity, responseType);
        return result;
    }
}
